<template>
  <a-drawer
    title="检查报表"
    :maskClosable="false"
    width=840
    placement="right"
    :closable="false"
    @close="onClose"
    :visible="reportAddVisiable"
    style="height: calc(100% - 55px);overflow: auto;padding-bottom: 53px;">
    <a-form :form="form">
      <a-form-item label='检查部门' v-bind="formItemLayout">
        <a-select :defaultValue="officeId" style="width: 200px" disabled>
          <a-select-option :value="officeId">{{officeName}}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='检查类型' v-bind="formItemLayout">
        <a-select
          showSearch
          placeholder="请选择检查类型"
          style="width: 200px"
          @change="handlechange"
          v-decorator="['reportType',{ initialValue:'1' },{rules: [{ required: true, message: '请选择检查类型' }]}]">
          <a-select-option value="1">例行检查</a-select-option>
          <a-select-option value="2">突击检查</a-select-option>
          <a-select-option value="3">特殊日期检查</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label='检查人' v-bind="formItemLayout">
         <a-input v-decorator="['reportPerson', {rules: [ {required: true, message: '请填写检查人'}]}]" style="width: 200px"/>
      </a-form-item>
    </a-form>
    <record-add
      @success="handleRecordAddSuccess"
      @close="handleRecordAddClose"
      :recordAddVisiable="recordAddVisiable"
      :report = report>
    </record-add>
    <div class="drawer-bootom-button">
      <a-popconfirm title="确定放弃录入？" @confirm="onClose" okText="确定" cancelText="取消">
        <a-button style="margin-right: .8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="showRecordAdd" type="primary" :loading="loading">开始录入<a-icon type="right" /></a-button>
    </div>
  </a-drawer>
</template>
<script>
const formItemLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 18 }
}
import RecordAdd from './RecordAdd'
export default {
  name: 'ReportAdd',
  components: {RecordAdd},
  props: {
    reportAddVisiable: {
      default: false
    }
  },
  data () {
    return {
      loading: false,
      formItemLayout,
      form: this.$form.createForm(this),
      report: {},
      checkedKeys: [],
      expandedKeys: [],
      officeId: [],
      officeName: [],
      recordAddVisiable: false,
      success : false
    }
  },
  methods: {
    
    handlechange(value){
      this.report.reportType = value
    },
    reset () {
      this.loading = false
      this.expandedKeys = this.checkedKeys = []
      this.report = {}
      this.form.resetFields()
    },
    handleRecordAddClose () {
      this.recordAddVisiable = false
    },
    handleRecordAddSuccess () {
      this.recordAddVisiable = false
      this.$message.success('新增检查报表成功')
    },
    showRecordAdd () {
      this.form.validateFields((err, values) => {
        if (!err) {
          this.setReportFields()
          this.recordAddVisiable = true
        }
      })
      
    },
    onClose () {
      this.reset()
      this.$emit('success')
    },
    handleCheck (checkedKeys) {
      this.checkedKeys = checkedKeys
    },
    handleExpand (expandedKeys) {
      this.expandedKeys = expandedKeys
    },
    setReportFields () {
      let values = this.form.getFieldsValue(['officeId', 'reportType', 'reportPerson'])
      if (typeof values !== 'undefined') {
        Object.keys(values).forEach(_key => { this.report[_key] = values[_key] })
      }
      let user = this.$db.get("USER")
      let company = this.$db.get("COMPANY")
      if (user.officeId === null || user.officeId === "" || user.officeId === undefined){
          this.report.reportSelf = '1'
      }else{
          this.report.reportSelf = '0'
      }
      this.report.companyId = company.value
      this.report.officeId = this.officeId;
      let reportTypeName = "" ;
      switch (values['reportType']){
        case '1':
          reportTypeName = '例行检查';break;
        case '2':
          reportTypeName = '突击检查';break;
        case '3':
          reportTypeName = '特殊日期检查';break;
        default :
        break;
      }
      this.report.officeName = this.officeName
      this.report.reportTypeName = reportTypeName
      this.report.createTime = new Date();
      this.report.reportScore = 100.0
    },
    filterOption (input, option) {
      return (
        option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    }
  },
  watch: {
    reportAddVisiable () {
      if (this.reportAddVisiable) {
        let user = this.$db.get("USER")
        this.officeId = user.officeId
        this.officeName = user.officeName
      }
    }
  }
}
</script>
